home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / security / shadow-3.1.4 / gsdbm.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-11-26  |  2.6 KB  |  145 lines

  1. /*
  2.  * Copyright 1990, 1991, John F. Haugh II
  3.  * All rights reserved.
  4.  *
  5.  * Permission is granted to copy and create derivative works for any
  6.  * non-commercial purpose, provided this copyright notice is preserved
  7.  * in all copies of source code, or included in human readable form
  8.  * and conspicuously displayed on all copies of object code or
  9.  * distribution media.
  10.  */
  11.  
  12. #ifndef    lint
  13. static    char    sccsid[] = "@(#)gsdbm.c    3.5    08:45:49    9/12/91";
  14. #endif
  15.  
  16. #include <string.h>
  17. #include <stdio.h>
  18. #include "shadow.h"
  19. #include "config.h"
  20.  
  21. #ifdef    NDBM
  22. #include <ndbm.h>
  23. DBM    *sgr_dbm;
  24.  
  25. #define    GRP_FRAG    256
  26.  
  27. /*
  28.  * sgr_dbm_update
  29.  *
  30.  * Updates the DBM password files, if they exist.
  31.  */
  32.  
  33. int
  34. sgr_dbm_update (sgr)
  35. struct    sgrp    *sgr;
  36. {
  37.     datum    key;
  38.     datum    content;
  39.     char    data[BUFSIZ*8];
  40.     char    sgrpkey[60];
  41.     char    *cp;
  42.     int    len;
  43.     int    i;
  44.     int    cnt;
  45.     static    int    once;
  46.  
  47.     if (! once) {
  48.         if (! sgr_dbm)
  49.             setsgent ();
  50.  
  51.         once++;
  52.     }
  53.     if (! sgr_dbm)
  54.         return 0;
  55.  
  56.     len = sgr_pack (sgr, data);
  57.  
  58.     if (len <= GRP_FRAG) {
  59.         content.dsize = len;
  60.         content.dptr = data;
  61.  
  62.         key.dsize = strlen (sgr->sg_name);
  63.         key.dptr = sgr->sg_name;
  64.         if (dbm_store (sgr_dbm, key, content, DBM_REPLACE))
  65.             return 0;
  66.     } else {
  67.         content.dsize = sizeof cnt;
  68.         content.dptr = (char *) &cnt;
  69.         cnt = (len + (GRP_FRAG-1)) / GRP_FRAG;
  70.  
  71.         key.dsize = strlen (sgr->sg_name);
  72.         key.dptr = sgr->sg_name;
  73.         if (dbm_store (sgr_dbm, key, content, DBM_REPLACE))
  74.             return 0;
  75.  
  76.         for (cp = data, i = 0;i < cnt;i++) {
  77.             content.dsize = len > GRP_FRAG ? GRP_FRAG:len;
  78.             len -= content.dsize;
  79.             content.dptr = cp;
  80.             cp += content.dsize;
  81.  
  82.             key.dsize = sizeof i + strlen (sgr->sg_name);
  83.             key.dptr = sgrpkey;
  84.             memcpy (sgrpkey, (char *) &i, sizeof i);
  85.             strcpy (sgrpkey + sizeof i, sgr->sg_name);
  86.             if (dbm_store (sgr_dbm, key, content, DBM_REPLACE))
  87.                 return 0;
  88.         }
  89.     }
  90.     return 1;
  91. }
  92.  
  93. /*
  94.  * sgr_dbm_remove
  95.  *
  96.  * Deletes the DBM shadow group file entries, if they exist.
  97.  */
  98.  
  99. int
  100. sgr_dbm_remove (name)
  101. char    *name;
  102. {
  103.     datum    key;
  104.     datum    content;
  105.     char    grpkey[60];
  106.     int    i;
  107.     int    cnt;
  108.     int    errors = 0;
  109.     static    int    once;
  110.  
  111.     if (! once) {
  112.         if (! sgr_dbm)
  113.             setsgent ();
  114.  
  115.         once++;
  116.     }
  117.     if (! sgr_dbm)
  118.         return 0;
  119.  
  120.     key.dsize = strlen (name);
  121.     key.dptr = name;
  122.     content = dbm_fetch (sgr_dbm, key);
  123.     if (content.dptr == 0)
  124.         ++errors;
  125.     else {
  126.         if (content.dsize == sizeof (int)) {
  127.             memcpy ((char *) &cnt, content.dptr, sizeof cnt);
  128.  
  129.             for (i = 0;i < cnt;i++) {
  130.                 key.dsize = sizeof i + strlen (name);
  131.                 key.dptr = grpkey;
  132.                 memcpy (grpkey, (char *) &i, sizeof i);
  133.                 strcpy (grpkey + sizeof i, name);
  134.                 if (dbm_delete (sgr_dbm, key))
  135.                     ++errors;
  136.             }
  137.         } else {
  138.             if (dbm_delete (sgr_dbm, key))
  139.                 ++errors;
  140.         }
  141.     }
  142.     return errors ? 0:1;
  143. }
  144. #endif
  145.